<template>
  <div class="page-container-padding" style="display:flex;">
    <!-- 方案发布 -->
    <div style="width:900px;margin-right:10px;">
      <el-form label-width="90px" ref="planForm" :model="planObj" :rules="planObjRules">
        <el-form-item label="方案标题" prop="title"><el-input v-model.trim="planObj.title" placeholder="xx品种做多/空/套利投资方案"></el-input></el-form-item>
        <el-form-item label="生效日期"><el-date-picker :clearable="false" placeholder="方案的生效日期" v-model="planObj.activa_date"></el-date-picker></el-form-item>
        <el-form-item label="结束日期"><el-date-picker :clearable="false" placeholder="方案的结束日期" v-model="planObj.expire_date"></el-date-picker></el-form-item>
        <el-form-item label="建仓均价" prop="build_price"><el-input v-model.trim="planObj.build_price" placeholder="填写方案中的计划的建仓价"></el-input></el-form-item>
        <el-form-item label="目标均价" prop="target_price"><el-input v-model.trim="planObj.target_price" placeholder="填写方案中的计划的目标价"></el-input></el-form-item>
        <el-form-item label="止损均价" prop="cutloss_price"><el-input v-model.trim="planObj.cutloss_price"  placeholder="填写方案中的计划的止损价"></el-input></el-form-item>
        <el-form-item label="所在小组">
          <el-select v-model="planObj.group_name" style="width:160px;">
            <el-option value="宏观金融" label="宏观金融"></el-option>
            <el-option value="农业产品" label="农业产品"></el-option>
            <el-option value="化工能源" label="化工能源"></el-option>
            <el-option value="黑色金属" label="黑色金属"></el-option>
          </el-select>
          <span style="color:#0073FC;margin-left:10px;">总资金和使用比例用于计算收益率，请仔细填写。</span>
        </el-form-item>
        <el-form-item label="总资金" prop="fund"><el-input v-model.trim="planObj.fund" placeholder="方案总资金，按比例计算之前的资金。单位：元"></el-input></el-form-item>
        <el-form-item label="使用比例" prop="fund_rate"><el-input v-model.trim="planObj.fund_rate" @blur="fundRateBlur" placeholder="输入方案资金占用比例(小数)"></el-input></el-form-item>
        <el-form-item label="方案类型" prop="category">
          <el-select v-model="planObj.category">
            <el-option value="单边" label="单边"></el-option>
            <el-option value="套利" label="套利"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="涉及品种" prop="varieties">
          <el-select v-model="planObj.varieties" multiple placeholder="请选择,多个可多选">
            <el-option v-for="item in authVarietyList" :value="item.variety_en" :label="item.variety_name" :key="item.variety_en"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="申请实操" prop="apply_firm"><el-radio v-model="planObj.apply_firm" :label="1">是</el-radio><el-radio v-model="planObj.apply_firm" :label="0">否</el-radio></el-form-item>
        <el-form-item label="方案文件">
          <el-upload action="''" accept=".pdf" :before-upload="()=>{return false}"
           :on-change="annexChange" :file-list="planAnnexFileList">
            <div class="el-upload__text"><em style="color: #0066FF;">点击选择</em> (只能上传pdf文件)</div>
          </el-upload>
        </el-form-item>
        <el-form-item><el-button type="primary" @click="publishExchangePlan" :loading="savePlanLoading">确认保存</el-button></el-form-item>
      </el-form>
    </div>
    <!-- 撰写说明 -->
    <div style="padding: 3px 5px;line-height:22px;max-width:450px;">
      <span class="explain-title">一、工作说明</span>
      <br>1、针对有中长线行情机会的品种，撰写投资计划。投资计划在《瑞智综合管理系统》填写发布并跟踪，并将投资计划文件上传至系统。
      <br><span class="explain-title">二、工作要求</span>
      <br>1、每个品种 3 个月至少发一篇投资计划。
      <br>2、投资方案撰写盈亏比需要达到 3:1 以上，且开平仓区间 6%以上。
      <br>3、方案发布后，在未建仓的情况下可有一次调整建仓点位和相应的止损、目标点位的机会。建仓后在未被止损的情况下，可在交易时间段内发布结束方案，收益率按平仓时间点的价位计算。
      <br>4、每个计划 100 万资金，自己安排入金比例，保证金按交易所两倍算。
      <br>5、每份投资计划周期超过 3 个月，没建到仓的算失效，建到仓的按三个月最后一天收盘价计算盈亏，方案发布后要及时跟踪，进入系统点选操作。
      <br>6、投资方案附件要上传的至系统。
      <br>7、要及时对投资计划进行跟踪及在系统中进行确认操作。
      <br><span class="explain-title">三、考核评价</span>
      <br>1、主要考虑收益率和方案撰写质量。
      <br>2、根据方案的逻辑性、数据论证度，可操作性、价位情况、语言组织等评审人员按 A、B、C、D、E 等级评价，符合五项的 A 级、四项B 级、三项 C 级、两项 D 级、一项 E 级。
      <br>3、收益率最终按人数排名比例分 A、B、C、D、E 五个等级。
      <br>4、部门可安排人员进行抽查验证，对有问题策略进行处罚。如弄虚做假；价格、时间点选不严谨等行为,被抽查不合格的记录不进行统计。
    </div>
  </div>
</template>

<script>
  let NAME_SPACE = 'V02'
  import { getUserAuthVariety } from '@/api/operations/variety'
  import { publishInvestmentPlan } from '@/api/departmentWork/investment'
  import { datetimeToStr } from '@/utils/datetimeUtils'
  export default {
    data(){
      return {
        authVarietyList: [],
        planObj: {
          title: '',
          activa_date: new Date(),
          expire_date: null,
          build_price: null,
          target_price: null,
          cutloss_price: null,
          group_name: '宏观金融',
          fund: null,
          fund_rate: null,
          category: '单边',
          varieties: [],
          apply_firm:1,
        },
        planObjRules: {
          title: [{required: true, message: '请填写方案标题', trigger: 'blur'}],
          build_price: [{required: true, message: '请填写建仓均价', trigger: 'blur'}],
          target_price: [{required: true, message: '请填写目标均价', trigger: 'blur'}],
          cutloss_price: [{required: true, message: '请填写止损均价', trigger: 'blur'}],
          fund: [{required: true, message: '请填写计划使用的总资金', trigger: 'blur'}],
          fund_rate: [{required: true, message: '请填写总资金的使用比例', trigger: 'blur'}],
          category: [{required: true, message: '方案类型不能为空', trigger: 'change'}],
          varieties: [{required: true, message: '涉及的品种不能为空', trigger: 'change'}],
          apply_firm: [{required: true, message: '申请实操为必选项', trigger: 'change'}],
        },
        planAnnexFile: null,
        planAnnexFileList: [],
        savePlanLoading: false
      }
    },
    mounted() {
      const today = new Date()
      this.planObj.expire_date = new Date(today.getFullYear(), today.getMonth() + 1, 0)
      this.getUserVariety()
    },
    methods: {
      fundRateBlur(value){
        let num = parseFloat(this.planObj.fund_rate)
        if (Number.isNaN(num) || num > 1 || num < 0) {
          this.planObj.fund_rate = 1
        } else {
          this.planObj.fund_rate = num
        }
      },

      annexChange(file, fileList) {
        this.planAnnexFileList = fileList.slice(-1)
        this.planAnnexFile = file.raw
      },
      // 使用的品种跟策略一样NAME_SPACE=02
      getUserVariety() {
        getUserAuthVariety({ space: NAME_SPACE }).then((res) => {
          this.authVarietyList = res.data
        })
      },
      // 发布方案
      publishExchangePlan() {
        // 验证是否选择附件
        if (!this.planAnnexFile) {
          this.$baseMessage('您还未选择方案附件!', 'error')
          return false
        }
        this.$refs.planForm.validate((valid) => {
          if (!valid){
            this.$baseMessage('请正确完整填写表单', 'error')
            return false
          }else{
            // 上传方案
            this.savePlanLoading = true
            const planJson = {
              title: this.planObj.title,
              active_date: datetimeToStr(this.planObj.activa_date, true, '-'),
              expire_date: datetimeToStr(this.planObj.expire_date, true, '-'),
              build_price: parseFloat(this.planObj.build_price),
              target_price: parseFloat(this.planObj.target_price),
              cutloss_price: parseFloat(this.planObj.cutloss_price),
              group_name: this.planObj.group_name,
              fund: parseFloat(this.planObj.fund),
              fund_rate: parseFloat(this.planObj.fund_rate),
              category: this.planObj.category,
              varieties: this.planObj.varieties,
              apply_firm: this.planObj.apply_firm,
            }
            const formData = new FormData()
            formData.append("file", this.planAnnexFile)
            formData.append("plan_doc", JSON.stringify(planJson))
            publishInvestmentPlan(formData).then(res => {
              this.planAnnexFileList = []
              this.planAnnexFile = null
              this.$baseMessage(res.msg, 'success')
              this.savePlanLoading = false
            }).catch(err=>{
              this.savePlanLoading = false
            })
          }
        })


      }
    }
  }
</script>

<style scoped>
.explain-title{
    font-weight:700;
    color: #006eff;
  }
</style>
